class SRScoreBoard extends KFScoreBoard;

var localized string NotShownInfo,PlayerCountText,SpectatorCountText,AliveCountText,BotText;
var localized string NameText;

simulated event UpdateScoreBoard(Canvas Canvas)
{
	local PlayerReplicationInfo PRI, OwnerPRI;
	local int i, FontReduction, NetXPos, PlayerCount, HeaderOffsetY, HeadFoot, MessageFoot, 
		PlayerBoxSizeY, BoxSpaceY, NameXPos, BoxTextOffsetY, OwnerOffset, HealthXPos, BoxXPos,
		KillsXPos, TitleYPos, BoxWidth, VetXPos, NotShownCount;
	local float XL,YL;
	local float KillsXL, NetXL, HealthXL, MaxNamePos, KillWidthX, TimeXPos;
	local Material VeterancyBox,StarBox;
	local string S;
	local byte Stars;
	local int PlayedTime;
	local int BrutesXPos,ScrakesXPos,FPsXPos,PatsXPos, PatDXPos; 
	local int ShieldXPos;
	local float ShieldWidthX, PatDXL, HealthWidthX, ShieldXL, NamesXL, TimeXL, 
				NamesWidthX, PatDWidthX, TimeWidthX;

	local float Shift8, Shift9, Shift11, Shift16;
	
	local int Shield;
	
	OwnerPRI = KFPlayerController(Owner).PlayerReplicationInfo;
	OwnerOffset = -1;
	
	for ( i = 0; i < GRI.PRIArray.Length; i++)
	{
		PRI = GRI.PRIArray[i];
		if ( !PRI.bOnlySpectator )
		{
			if( !PRI.bOutOfLives && KFPlayerReplicationInfo(PRI).PlayerHealth>0 )
				++HeadFoot;
			if ( PRI == OwnerPRI )
				OwnerOffset = i;
			PlayerCount++;
		}
		else if(PRI.PlayerName ~= "WebAdmin" || 
				PRI.PlayerName ~= "DemoRecSpectator" ||
				PRI.PlayerName~="ChatterSpectator" ||
				(PRI.bAdmin && PRI.bOnlySpectator)
				)
		{
			continue;
		}
		else
		{
			++NetXPos;
		}
	}

	// First, draw title.
	S = SkillLevel[Clamp(InvasionGameReplicationInfo(GRI).BaseDifficulty, 0, 7)] @ "|" @ WaveString @ (InvasionGameReplicationInfo(GRI).WaveNumber + 1) @ "|" @ Level.Title @ "|" @ FormatTime(GRI.ElapsedTime);

	Canvas.Font = class'ROHud'.static.GetSmallMenuFont(Canvas);
	Canvas.TextSize(S, XL,YL);
	Canvas.DrawColor = HUDClass.default.RedColor;
	Canvas.Style = ERenderStyle.STY_Normal;

	HeaderOffsetY = Canvas.ClipY * 0.11;
	Canvas.SetPos(0.5 * (Canvas.ClipX - XL), HeaderOffsetY);
	Canvas.DrawTextClipped(S);

	// Second title line
	S = PlayerCountText@PlayerCount@SpectatorCountText@NetXPos@AliveCountText@HeadFoot;
	Canvas.TextSize(S, XL,YL);
	HeaderOffsetY+=YL;
	Canvas.SetPos(0.5 * (Canvas.ClipX - XL), HeaderOffsetY);
	Canvas.DrawTextClipped(S);
	HeaderOffsetY+=(YL*3.f);

	// Select best font size and box size to fit as many players as possible on screen
	if ( Canvas.ClipX < 600 )
		i = 4;
	else if ( Canvas.ClipX < 800 )
		i = 3;
	else if ( Canvas.ClipX < 1000 )
		i = 2;
	else if ( Canvas.ClipX < 1200 )
		i = 1;
	else i = 0;

	Canvas.Font = class'ROHud'.static.LoadMenuFontStatic(i);
	Canvas.TextSize("Test", XL, YL);
	PlayerBoxSizeY = 1.5 * YL;
	BoxSpaceY = 0.25 * YL;

	while( ((PlayerBoxSizeY+BoxSpaceY)*PlayerCount)>(Canvas.ClipY-HeaderOffsetY) )
	{
		if( ++i>=5 || ++FontReduction>=3 ) // Shrink font, if too small then break loop.
		{
			// We need to remove some player names here to make it fit.
			NotShownCount = PlayerCount-int((Canvas.ClipY-HeaderOffsetY)/(PlayerBoxSizeY+BoxSpaceY))+1;
			PlayerCount-=NotShownCount;
			break;
		}
		Canvas.Font = class'ROHud'.static.LoadMenuFontStatic(i);
		Canvas.TextSize("Test", XL, YL);
		PlayerBoxSizeY = 1.5 * YL;
		BoxSpaceY = 0.25 * YL;
	}

	HeadFoot = 7 * YL;
	MessageFoot = 1.5 * HeadFoot;

	BoxWidth = 0.9 * Canvas.ClipX;
	BoxXPos = 0.5 * (Canvas.ClipX - BoxWidth);
	BoxWidth = Canvas.ClipX - 2 * BoxXPos;
	VetXPos = BoxXPos + 0.00005 * BoxWidth;

	
	
	Shift8=0.07;
	Shift9=0.07;
	Shift11=0.10;
	Shift16=0.14;

	NameXPos = VetXPos + Shift8 * BoxWidth;
	TimeXPos = NameXPos + (Shift16 + Shift9) * BoxWidth;
	KillsXPos = TimeXPos + Shift8 * BoxWidth;
    BrutesXPos = KillsXPos + Shift11 * BoxWidth; 
    ScrakesXPos = BrutesXPos + Shift8 * BoxWidth;
	FPsXPos = ScrakesXPos + Shift8 * BoxWidth;
    PatsXPos = FPsXPos + Shift8 * BoxWidth;
	PatDXPos = PatsXPos + Shift8 * BoxWidth;
	HealthXpos = PatDXPos + Shift8 * BoxWidth;
	ShieldXpos = HealthXpos + Shift9 * BoxWidth;
	NetXPos = ShieldXpos + Shift9 * BoxWidth;

	// draw background boxes
	Canvas.Style = ERenderStyle.STY_Alpha;
	Canvas.DrawColor = HUDClass.default.WhiteColor;
	Canvas.DrawColor.A = 128;

	for ( i = 0; i < PlayerCount; i++ )
	{
		Canvas.SetPos(BoxXPos, HeaderOffsetY + (PlayerBoxSizeY + BoxSpaceY) * i);
		Canvas.DrawTileStretched( BoxMaterial, BoxWidth, PlayerBoxSizeY);
	}
	if( NotShownCount>0 ) // Add box for not shown players.
	{
		Canvas.DrawColor = HUDClass.default.RedColor;
		Canvas.SetPos(BoxXPos, HeaderOffsetY + (PlayerBoxSizeY + BoxSpaceY) * PlayerCount);
		Canvas.DrawTileStretched( BoxMaterial, BoxWidth, PlayerBoxSizeY);
		Canvas.DrawColor = HUDClass.default.WhiteColor;
	}

	// Draw headers
	TitleYPos = HeaderOffsetY - 1.1 * YL;

	
	//Canvas.StrLen(DeathsText, DeathsXL, YL);

	Canvas.DrawColor = HUDClass.default.WhiteColor;

	Canvas.StrLen("Name", NamesXL, YL);
	Canvas.SetPos(NameXPos + ((Shift16 + Shift9) * BoxWidth - NamesXL) * 0.5 , TitleYPos);
	Canvas.DrawText("Name",true);

	Canvas.StrLen("Time", TimeXL, YL);
	Canvas.SetPos(TimeXPos + (Shift8 * BoxWidth - TimeXL) * 0.5 , TitleYPos);
	Canvas.DrawText("Time",true);

	Canvas.StrLen(KillsText, KillsXL, YL);
	Canvas.SetPos(KillsXPos + (Shift11 * BoxWidth - KillsXL) * 0.5 , TitleYPos);
	Canvas.DrawText(KillsText,true);

	Canvas.StrLen("BR", KillsXL, YL);
	Canvas.SetPos(BrutesXPos + (Shift8 * BoxWidth - KillsXL) * 0.5 , TitleYPos);
	Canvas.DrawText("BR",true);

	Canvas.StrLen("SC", KillsXL, YL);
	Canvas.SetPos(ScrakesXPos + (Shift8 * BoxWidth - KillsXL) * 0.5 , TitleYPos);
	Canvas.DrawText("SC",true);

	Canvas.StrLen("FP", KillsXL, YL);
	Canvas.SetPos(FPsXPos + (Shift8 * BoxWidth - KillsXL) * 0.5 , TitleYPos);
	Canvas.DrawText("FP",true);

	Canvas.StrLen("P", KillsXL, YL);
	Canvas.SetPos(PatsXPos + (Shift8 * BoxWidth - KillsXL) * 0.5 , TitleYPos);
	Canvas.DrawText("P",true);

	Canvas.StrLen("Pat D", PatDXL, YL);
	Canvas.SetPos(PatDXPos + (Shift9 * BoxWidth - PatDXL) * 0.5, TitleYPos);
	Canvas.DrawText("Pat D",true);

	Canvas.StrLen("HP", HealthXL, YL);
	Canvas.SetPos(HealthXPos + (Shift9 * BoxWidth - HealthXL) * 0.5 , TitleYPos);
	Canvas.DrawText("HP",true);

	Canvas.StrLen("SH", ShieldXL, YL);
	Canvas.SetPos(ShieldXPos + (Shift9 * BoxWidth - ShieldXL) * 0.5 , TitleYPos);
	Canvas.DrawText("SH",true);

	Canvas.Style = ERenderStyle.STY_Normal;
	Canvas.DrawColor = HUDClass.default.WhiteColor;
	Canvas.SetPos(0.5 * Canvas.ClipX, HeaderOffsetY + 4);

	Canvas.StrLen(NetText, NetXL, YL);
	Canvas.SetPos(NetXPos + (Shift9 * BoxWidth - NetXL) * 0.5 , TitleYPos);
	Canvas.DrawText(NetText,true);
	//Canvas.DrawTextClipped(NetText);

	BoxTextOffsetY = HeaderOffsetY + 0.5 * (PlayerBoxSizeY - YL);

	Canvas.DrawColor = HUDClass.default.WhiteColor;
	MaxNamePos = Canvas.ClipX;
	//MaxNamePos = 0.5 * (TimeXPos - NameXPos);
	Canvas.ClipX = TimeXPos - 4.f;

	for ( i = 0; i < PlayerCount; i++ )
	{
		Canvas.StrLen(GRI.PRIArray[i].PlayerName, NamesWidthX, YL);
		Canvas.SetPos(NameXPos + ((Shift16 + Shift9) * BoxWidth - NamesWidthX) * 0.5 , (PlayerBoxSizeY + BoxSpaceY) * i + BoxTextOffsetY);

		if( i == OwnerOffset )
		{
			Canvas.DrawColor.G = 0;
			Canvas.DrawColor.B = 0;
		}
		else
		{
			Canvas.DrawColor.G = 255;
			Canvas.DrawColor.B = 255;
		}
		Canvas.DrawTextClipped(GRI.PRIArray[i].PlayerName);
	}
	if( NotShownCount>0 ) // Draw not shown info
	{
		Canvas.DrawColor.G = 255;
		Canvas.DrawColor.B = 0;
		Canvas.SetPos(NameXPos, (PlayerBoxSizeY + BoxSpaceY)*PlayerCount + BoxTextOffsetY);
		Canvas.DrawText(NotShownCount@NotShownInfo,true);
	}

	Canvas.ClipX = MaxNamePos;
	Canvas.DrawColor = HUDClass.default.WhiteColor;

	Canvas.Style = ERenderStyle.STY_Normal;

	// Draw the player informations.
	for ( i = 0; i < PlayerCount; i++ )
	{
	
		PRI = GRI.PRIArray[i];
		Canvas.DrawColor = HUDClass.default.WhiteColor;
		//Canvas.DrawColor.A = 25;

		// Display perks.
		if ( KFPlayerReplicationInfo(PRI)!=None && 
			Class<SRVeterancyTypes>(KFPlayerReplicationInfo(PRI).ClientVeteranSkill)!=none )
		{
			Stars = Class<SRVeterancyTypes>(KFPlayerReplicationInfo(PRI).ClientVeteranSkill).Static.PreDrawPerk(Canvas
				,KFPlayerReplicationInfo(PRI).ClientVeteranSkillLevel,VeterancyBox,StarBox);

			if ( VeterancyBox != None )
				DrawPerkWithStars(Canvas,VetXPos,HeaderOffsetY+(PlayerBoxSizeY+BoxSpaceY)*i,PlayerBoxSizeY,Stars,VeterancyBox,StarBox);
			Canvas.DrawColor = HUDClass.default.WhiteColor;
		}

		// draw time
		if( GRI.ElapsedTime<PRI.StartTime ) // Login timer error, fix it.
			GRI.ElapsedTime = PRI.StartTime;
		if(!PRI.bIsSpectator && !PRI.bOutOfLives)
		{
			PlayedTime=Min(GRI.ElapsedTime-PRI.StartTime,SRPlayerReplicationInfo(PRI).TimePlayedOnMap);
		}
		else
		{
			PlayedTime=SRPlayerReplicationInfo(PRI).TimePlayedOnMap;
		}
		
		S = FormatTime(PlayedTime);
		Canvas.StrLen(S, TimeWidthX, YL);
		Canvas.SetPos(TimeXPos + (Shift8 * BoxWidth - TimeWidthX) * 0.5 , (PlayerBoxSizeY + BoxSpaceY) * i + BoxTextOffsetY);
		Canvas.DrawText(S, true);
		Canvas.DrawColor = HUDClass.default.WhiteColor;
		
		//Log(S);
		
		Canvas.SetDrawColor(72, 72, 255, 255);
		Canvas.StrLen(KFPlayerReplicationInfo(PRI).Kills, KillWidthX, YL);
		Canvas.SetPos(KillsXPos + (Shift11 * BoxWidth - KillWidthX) * 0.5 , (PlayerBoxSizeY + BoxSpaceY) * i + BoxTextOffsetY);
		Canvas.DrawText(KFPlayerReplicationInfo(PRI).Kills, true);
		Canvas.DrawColor = HUDClass.default.WhiteColor;
		//Log(KFPlayerReplicationInfo(PRI).Kills);
		
		Canvas.SetDrawColor(255, 255, 0, 255);
		Canvas.StrLen(SRPlayerReplicationInfo(PRI).KilledBrutes, KillWidthX, YL);
		Canvas.SetPos(BrutesXPos + (Shift8 * BoxWidth - KillWidthX) * 0.5 , (PlayerBoxSizeY + BoxSpaceY) * i + BoxTextOffsetY);
		Canvas.DrawText(SRPlayerReplicationInfo(PRI).KilledBrutes, true);
		Canvas.DrawColor = HUDClass.default.WhiteColor;
		//Log(SRPlayerReplicationInfo(PRI).KilledBrutes);

		Canvas.SetDrawColor(0, 255, 64, 255);
		Canvas.StrLen(SRPlayerReplicationInfo(PRI).KilledScrakes, KillWidthX, YL);
		Canvas.SetPos(ScrakesXPos + (Shift8 * BoxWidth - KillWidthX) * 0.5 , (PlayerBoxSizeY + BoxSpaceY) * i + BoxTextOffsetY);
		Canvas.DrawText(SRPlayerReplicationInfo(PRI).KilledScrakes, true);
		Canvas.DrawColor = HUDClass.default.WhiteColor;

		Canvas.SetDrawColor(255, 72, 72, 255);
		Canvas.StrLen(SRPlayerReplicationInfo(PRI).KilledFPs, KillWidthX, YL);
		Canvas.SetPos(FPsXPos + (Shift8 * BoxWidth - KillWidthX) * 0.5 , (PlayerBoxSizeY + BoxSpaceY) * i + BoxTextOffsetY);
		Canvas.DrawText(SRPlayerReplicationInfo(PRI).KilledFPs, true);
		Canvas.DrawColor = HUDClass.default.WhiteColor;

		Canvas.SetDrawColor(255, 0, 255, 255);
		Canvas.StrLen(SRPlayerReplicationInfo(PRI).KilledPats, KillWidthX, YL);
		Canvas.SetPos(PatsXPos + (Shift8 * BoxWidth - KillWidthX) * 0.5 , (PlayerBoxSizeY + BoxSpaceY) * i + BoxTextOffsetY);
		Canvas.DrawText(SRPlayerReplicationInfo(PRI).KilledPats, true);
		Canvas.DrawColor = HUDClass.default.WhiteColor;

		Canvas.SetDrawColor(255, 0, 255, 255);
		Canvas.StrLen(SRPlayerReplicationInfo(PRI).DamageToPat, PatDWidthX, YL);
		Canvas.SetPos(PatDXPos + (Shift9 * BoxWidth - PatDWidthX) * 0.5 , (PlayerBoxSizeY + BoxSpaceY) * i + BoxTextOffsetY);
		Canvas.DrawText(SRPlayerReplicationInfo(PRI).DamageToPat, true);
		Canvas.DrawColor = HUDClass.default.WhiteColor;

		Canvas.StrLen(KFPlayerReplicationInfo(PRI).PlayerHealth, HealthWidthX, YL);
		Canvas.SetPos(HealthXPos + (Shift9 * BoxWidth - HealthWidthX) * 0.5 , (PlayerBoxSizeY + BoxSpaceY) * i + BoxTextOffsetY);
		
		if ( PRI.bOutOfLives )
		{
			Canvas.DrawColor = HUDClass.default.RedColor;
			Canvas.DrawText(OutText,true);
		}
		else
		{
			if( KFPlayerReplicationInfo(PRI).PlayerHealth>=95 )
			{
				Canvas.DrawColor = HUDClass.default.GreenColor;
			}
			else if( KFPlayerReplicationInfo(PRI).PlayerHealth>=50 )
			{
				Canvas.DrawColor = HUDClass.default.GoldColor;
			}
			else
			{
				Canvas.DrawColor = HUDClass.default.RedColor;
			}
			Canvas.DrawText(KFPlayerReplicationInfo(GRI.PRIArray[i]).PlayerHealth);
		}
		
		// draw shields
		//C = Controller(KFPlayerReplicationInfo(GRI.PRIArray[i]).Owner);
		//P=C.Pawn;
		Shield=SRPlayerReplicationInfo(PRI).Shield;
		Canvas.StrLen(Shield, ShieldWidthX, YL);
		Canvas.SetPos(ShieldXPos + (Shift9 * BoxWidth - ShieldWidthX) * 0.5 , (PlayerBoxSizeY + BoxSpaceY) * i + BoxTextOffsetY);

		if ( PRI.bOutOfLives )
		{
			Canvas.DrawColor = HUDClass.default.RedColor;
			Canvas.DrawText("",true);
		}
		else
		{
			if( Shield>=95 )
			{
				Canvas.DrawColor = HUDClass.default.GreenColor;
			}
			else if( Shield>=50 )
			{
				Canvas.DrawColor = HUDClass.default.GoldColor;
			}
			else
			{
				Canvas.DrawColor = HUDClass.default.RedColor;
			}
			Canvas.DrawText(Shield, true);
		}

		// Draw ping
		if( PRI.bAdmin )
		{
			Canvas.DrawColor = HUDClass.default.RedColor;
			S = string(PRI.Ping * 4);
		}
		else if ( !GRI.bMatchHasBegun )
		{
			if ( PRI.bReadyToPlay )
				S = ReadyText;
			else S = NotReadyText;
		}
		else if( !PRI.bBot )
			S = string(PRI.Ping * 4);
		else S = BotText;
		
		Canvas.StrLen(S, NetXL, YL);
		Canvas.DrawColor = HUDClass.default.WhiteColor;
		Canvas.SetPos(NetXPos + (Shift9 * BoxWidth - NetXL) * 0.5 , (PlayerBoxSizeY + BoxSpaceY) * i + BoxTextOffsetY);
		Canvas.DrawText(S,true);
	}
}

simulated final function DrawPerkWithStars( Canvas C, float X, float Y, float Scale, int Stars, Material PerkIcon, Material StarIcon )
{
	local byte i;

	C.SetPos(X,Y);
	C.DrawTile(PerkIcon, Scale, Scale, 0, 0, PerkIcon.MaterialUSize(), PerkIcon.MaterialVSize());
	Y+=Scale * 0.9f;
	X+=Scale * 0.92f;
	Scale*=0.2f;
	while( Stars>0 )
	{
		for( i=1; i<=Min(5,Stars); ++i )
		{
			C.SetPos(X,Y-(i*Scale*0.8f));
			C.DrawTile(StarIcon, Scale, Scale, 0, 0, StarIcon.MaterialUSize(), StarIcon.MaterialVSize());
		}
		X+=Scale;
		Stars-=5;
	}
}

defaultproperties
{
     NotShownInfo="player names not shown"
     PlayerCountText="Players:"
     SpectatorCountText="| Spectators:"
     AliveCountText="| Alive players:"
     BotText="BOT"
     NameText="Name"
     HealthyString="HP"
     TimeText="Time"
}
